﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using BrnMall.Core;
using BrnMall.Manager.Manager.Cart;
using BrnMall.Manager.Manager.Product;
using BrnMall.Manager.Manager.Restaurant;
using BrnMall.Services;
using QIDong.WeApp.Models;
using WeiTap.Restful;

namespace QIDong.WeApp.Controllers
{
    public class CartController : WebApi
    {
        /// <summary>
        /// 购物车
        /// </summary>
        /// <param name="uid">用户ID</param>
        /// <returns></returns>
        public CartModel Index(int uid)
        {
            try
            {
                //购物车商品列表
                List<OrderProductInfo> orderProductList = Carts.GetCartProductList(uid);
                //商品数量
                int pCount = Carts.SumOrderProductCount(orderProductList);
                //店铺购物车列表
                List<StoreCartInfo> storeCartList = Carts.TidyMallOrderProductList(orderProductList);

                //商品总数量
                int totalCount = Carts.SumMallCartOrderProductCount(storeCartList);
                //商品合计
                decimal productAmount = Carts.SumMallCartOrderProductAmount(storeCartList);
                //满减折扣
                int fullCut = Carts.SumMallCartFullCut(storeCartList);
                //订单合计
                decimal orderAmount = productAmount - fullCut;

                CartModel model = new CartModel
                {
                    TotalCount = totalCount,
                    ProductAmount = productAmount,
                    FullCut = fullCut,
                    OrderAmount = orderAmount,
                    StoreCartList = storeCartList
                };
                return model;
            }
            catch (Exception e)
            {
                Trace.TraceError(e.StackTrace);
                throw;
            }

        }

        /// <summary>
        /// 购物车
        /// </summary>
        /// <param name="sid">唯一标识符（餐位ID）</param>
        /// <param name="uid">用户ID</param>
        /// <returns></returns>
        public CartModel SessionIndex(string sid)
        {
            if (string.IsNullOrEmpty(sid)) throw new ApiException(ExceptionCode.DataError);
            try
            {
                //购物车商品列表
                List<OrderProductInfo> orderProductList = Carts.GetCartProductList(sid).Where(t => !CartManager.CheckIsPrint(t.RecordId)).ToList();
                //商品数量
                int pCount = Carts.SumOrderProductCount(orderProductList);
                //店铺购物车列表
                List<StoreCartInfo> storeCartList = Carts.TidyMallOrderProductList(orderProductList);

                //商品总数量
                int totalCount = Carts.SumMallCartOrderProductCount(storeCartList);
                //商品合计
                decimal productAmount = Carts.SumMallCartOrderProductAmount(storeCartList);
                //满减折扣
                int fullCut = Carts.SumMallCartFullCut(storeCartList);
                //订单合计
                decimal orderAmount = productAmount - fullCut;

                CartModel model = new CartModel
                {
                    TotalCount = totalCount,
                    ProductAmount = productAmount,
                    FullCut = fullCut,
                    OrderAmount = orderAmount,
                    StoreCartList = storeCartList
                };
                return model;
            }
            catch (Exception e)
            {
                Trace.TraceError(e.StackTrace);
                throw;
            }

        }
        /// <summary>
        /// 获取当前购物车已经打印确认的记录总数
        /// </summary>
        /// <param name="sid">唯一标示</param>
        /// <returns></returns>
        public int GetSessionCartPrintCount(string sid)
        {
            return CartManager.GetIsPrintCountBySid(sid);
        }

        /// <summary>
        /// 设置折扣
        /// </summary>
        /// <param name="storeId">店铺ID</param>
        /// <param name="sid">餐位编号</param>
        /// <param name="discount">折扣</param>

        public bool SetSessionDiscount(int storeId, string sid, decimal discount)
        {
            int id =0;
            if (int.TryParse(sid, out id))
            {
                DiningTableManager.SetCurrDiscount(id, discount);
                return true;
            }
            return false;



        }

        /// <summary>
        /// 清空购物车
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public bool Clear(int uid)
        {
            try
            {
                Carts.ClearCart(uid);
                return true;
            }
            catch (Exception e)
            {
                Trace.TraceError(e.Message);
                throw;
            }
        }
        /// <summary>
        /// 清空购物车
        /// </summary>
        /// <param name="sid"></param>
        /// <returns></returns>
        public bool SessionClear(string sid)
        {
            if (string.IsNullOrEmpty(sid)) throw new ApiException(ExceptionCode.DataError);
            try
            {
                Carts.ClearCart(sid);
                return true;
            }
            catch (Exception e)
            {
                Trace.TraceError(e.Message);
                throw;
            }
        }
        /// <summary>
        /// 添加商品到购物车
        /// </summary>
        /// <param name="pid"></param>
        /// <param name="buyCount"></param>
        /// <param name="uid"></param>
        /// <returns></returns>
        public int AddProduct(int pid, int buyCount, int uid)
        {
            try
            {


                //判断商品是否存在
                PartProductInfo partProductInfo = Products.GetPartProductById(pid);
                if (partProductInfo == null)
                    throw new Exception("请选择商品");

                //店铺信息
                StoreInfo storeInfo = Stores.GetStoreById(partProductInfo.StoreId);
                if (storeInfo.State != (int)StoreState.Open)
                    throw new Exception("请选择商品");

                //购买数量不能小于1
                if (buyCount < 1)
                    throw new Exception("请填写购买数量");

                //商品库存
                int stockNumber = Products.GetProductStockNumberByPid(pid);
                if (stockNumber < buyCount)
                    throw new Exception("商品库存不足");

                //购物车中已经存在的商品列表
                List<OrderProductInfo> orderProductList = Carts.GetCartProductList(uid);
                OrderProductInfo orderProductInfo = Carts.GetCommonOrderProductByPid(pid, orderProductList);

                buyCount = orderProductInfo == null ? buyCount : orderProductInfo.BuyCount + buyCount;
                //将商品添加到购物车
                Carts.AddProductToCart(ref orderProductList, buyCount, partProductInfo, "", uid, DateTime.Now);
                //返回购物车中商品数量写
                return Carts.SumOrderProductCount(orderProductList);
            }
            catch (Exception e)
            {
                Trace.TraceError(e.StackTrace);
                throw;
            }
        }
        /// <summary>
        /// 添加商品到购物车
        /// </summary>
        /// <param name="pid"></param>
        /// <param name="buyCount"></param>
        /// <param name="sid"></param>
        /// <returns></returns>
        public int AddSessionProduct(int pid, int buyCount, string sid)
        {
            if (string.IsNullOrEmpty(sid)) throw new ApiException(ExceptionCode.DataError);
            try
            {


                //判断商品是否存在
                PartProductInfo partProductInfo = Products.GetPartProductById(pid);
                if (partProductInfo == null)
                    throw new Exception("请选择商品");

                //店铺信息
                StoreInfo storeInfo = Stores.GetStoreById(partProductInfo.StoreId);
                if (storeInfo.State != (int)StoreState.Open)
                    throw new Exception("请选择商品");

                //购买数量不能小于1
                if (buyCount < 1)
                    throw new Exception("请填写购买数量");

                //商品库存
                int stockNumber = Products.GetProductStockNumberByPid(pid);
                if (stockNumber < buyCount)
                    throw new Exception("商品库存不足");

                //购物车中已经存在的商品列表
                List<OrderProductInfo> orderProductList = Carts.GetCartProductList(sid).Where(t => !CartManager.CheckIsPrint(t.RecordId)).ToList(); 
                if(orderProductList.Count==0)
                    DiningTableManager.ClearCurrDiscount(Convert.ToInt32(sid));
                OrderProductInfo orderProductInfo = Carts.GetCommonOrderProductByPid(pid, orderProductList);
                buyCount = orderProductInfo?.BuyCount + buyCount ?? buyCount;
                //将商品添加到购物车
                Carts.AddProductToCart(ref orderProductList, buyCount, partProductInfo, sid, 0, DateTime.Now);
                //返回购物车中商品数量写
                return Carts.SumOrderProductCount(orderProductList);
            }
            catch (Exception e)
            {
                Trace.TraceError(e.StackTrace);
                throw;
            }
        }
        /// <summary>
        /// 修改购物车中商品数量
        /// </summary>
        /// <param name="pid">产品ID</param>
        /// <param name="buyCount">数量</param>
        /// <param name="uid">用户ID</param>
        /// <param name="selList">选中的购物车项键列表</param>
        /// <returns></returns>
        public CartModel ChangeProductCount(int pid, int buyCount, int uid, string selList)
        {
            try
            {
                //购物车商品列表
                List<OrderProductInfo> orderProductList = Carts.GetCartProductList(uid);
                //对应商品
                OrderProductInfo orderProductInfo = Carts.GetCommonOrderProductByPid(pid, orderProductList);
                if (orderProductInfo != null)//当商品已经存在
                {
                    if (buyCount < 1)//当购买数量小于1时，删除此商品
                    {
                        Carts.DeleteCartProduct(ref orderProductList, orderProductInfo);
                    }
                    else if (buyCount != orderProductInfo.BuyCount)
                    {
                        Carts.AddExistProductToCart(ref orderProductList, buyCount, orderProductInfo, DateTime.Now);
                    }
                }

                //商品数量
                int pCount = Carts.SumOrderProductCount(orderProductList);
                //店铺购物车列表
                List<StoreCartInfo> storeCartList = Carts.TidyMallOrderProductList(StringHelper.SplitString(selList), orderProductList);

                //商品总数量
                int totalCount = Carts.SumMallCartOrderProductCount(storeCartList);
                //商品合计
                decimal productAmount = Carts.SumMallCartOrderProductAmount(storeCartList);
                //满减折扣
                int fullCut = Carts.SumMallCartFullCut(storeCartList);
                //订单合计
                decimal orderAmount = productAmount - fullCut;

                CartModel model = new CartModel
                {
                    TotalCount = totalCount,
                    ProductAmount = productAmount,
                    FullCut = fullCut,
                    OrderAmount = orderAmount,
                    StoreCartList = storeCartList
                };
                //返回将购物车中商品数量
                return model;

            }
            catch (Exception e)
            {
                Trace.TraceError(e.StackTrace);
                throw;
            }
        }
        /// <summary>
        /// 修改购物车中商品数量
        /// </summary>
        /// <param name="pid">产品ID</param>
        /// <param name="buyCount">数量</param>
        /// <param name="sid">全局唯一标识符</param>
        /// <param name="selList">选中的购物车项键列表</param>
        /// <returns></returns>
        public CartModel ChangeSessionProductCount(int pid, int buyCount, string sid, string selList)
        {
            if (string.IsNullOrEmpty(sid)) throw new ApiException(ExceptionCode.DataError);
            try
            {
                //购物车商品列表
                List<OrderProductInfo> orderProductList = Carts.GetCartProductList(sid).Where(t => !CartManager.CheckIsPrint(t.RecordId)).ToList();
                //对应商品
                OrderProductInfo orderProductInfo = Carts.GetCommonOrderProductByPid(pid, orderProductList);
                if (orderProductInfo != null)//当商品已经存在
                {
                    if (buyCount < 1)//当购买数量小于1时，删除此商品
                    {
                        Carts.DeleteCartProduct(ref orderProductList, orderProductInfo);
                    }
                    else if (buyCount != orderProductInfo.BuyCount)
                    {
                        Carts.AddExistProductToCart(ref orderProductList, buyCount, orderProductInfo, DateTime.Now);
                    }
                }

                //商品数量
                int pCount = Carts.SumOrderProductCount(orderProductList);
                //店铺购物车列表
                List<StoreCartInfo> storeCartList = Carts.TidyMallOrderProductList(StringHelper.SplitString(selList), orderProductList);

                //商品总数量
                int totalCount = Carts.SumMallCartOrderProductCount(storeCartList);
                //商品合计
                decimal productAmount = Carts.SumMallCartOrderProductAmount(storeCartList);
                //满减折扣
                int fullCut = Carts.SumMallCartFullCut(storeCartList);
                //订单合计
                decimal orderAmount = productAmount - fullCut;

                CartModel model = new CartModel
                {
                    TotalCount = totalCount,
                    ProductAmount = productAmount,
                    FullCut = fullCut,
                    OrderAmount = orderAmount,
                    StoreCartList = storeCartList
                };
                //返回将购物车中商品数量
                return model;

            }
            catch (Exception e)
            {
                Trace.TraceError(e.StackTrace);
                throw;
            }
        }
        #region 商品预定，类似酒店预定这种循环库存

        /// <summary>
        /// 添加商品到购物车,酒店预约专用
        /// </summary>
        /// <param name="pid"></param>
        /// <param name="bookNumber"></param>
        /// <param name="uid"></param>
        /// <param name="startTime">商品的购买时间段</param>
        /// <param name="endTime">商品的购买时间段</param>
        /// <returns></returns>
        public int AddProductForBook(int pid, int uid, int bookNumber, string startTime, string endTime)
        {
            try
            {
                var bSTime = new DateTime(1900, 1, 1);
                var bETime = new DateTime(1900, 1, 1);
                if (!string.IsNullOrEmpty(startTime) && !string.IsNullOrEmpty(endTime))
                {
                    DateTime.TryParse(startTime, out bSTime);
                    DateTime.TryParse(endTime, out bETime);
                }
                else
                {
                    throw new Exception("请选择预定时间");
                }
                var days = (bETime.Date - bSTime.Date).Days;
                if (days <= 0)
                    throw new Exception("请检查预定时间");
                //判断商品是否存在
                PartProductInfo partProductInfo = Products.GetPartProductById(pid);
                if (partProductInfo == null)
                    throw new Exception("请选择商品");
                //店铺信息
                StoreInfo storeInfo = Stores.GetStoreById(partProductInfo.StoreId);
                if (storeInfo.State != (int)StoreState.Open)
                    throw new Exception("请选择商品");

                //购买数量不能小于1
                if (bookNumber < 1)
                    throw new Exception("请检查产品数量");

                //商品库存
                var stock = Products.GetProductStockByPid(pid);
                for (int i = 0; i < days; i++)
                {
                    var date = bSTime.AddDays(i);
                    var history = ProductBookStockManager.GetBookStock(pid, date);
                    if (stock.RecycleNumber < history)
                        throw new Exception("商品预定日期：" + date.ToShortDateString() + "库存不足");
                }

                //按日期获取价格
                var listPrice = ProductPriceManager.GetListByDate(pid, bSTime.ToString("yyyyMMdd"), bETime.ToString("yyyyMMdd"));
                if (listPrice != null && listPrice.Count > 0)
                {
                    partProductInfo.ShopPrice = listPrice[0].Price;                  
                }


                //购物车中已经存在的商品列表
                List<OrderProductInfo> orderProductList = Carts.GetCartProductList(uid);
                OrderProductInfo orderProductInfo = Carts.GetCommonOrderProductByPid(pid, orderProductList);

                if (orderProductInfo != null)
                {
                    Carts.DeleteCartProduct(ref orderProductList, orderProductInfo);//清除之前的预约记录
                }

                //将商品添加到购物车
                Carts.AddProductToCartForBook(ref orderProductList, bookNumber * days, partProductInfo, "", uid, DateTime.Now, bSTime, bETime, bookNumber);
                //返回购物车中商品数量写
                return Carts.SumOrderProductCount(orderProductList);
            }
            catch (Exception e)
            {
                Trace.TraceError(e.StackTrace);
                throw;
            }
        }
        /// <summary>
        /// 修改购物车中商品数量
        /// </summary>
        /// <param name="pid">产品ID</param>
        /// <param name="bookNumber">数量</param>
        /// <param name="uid">用户ID</param>
        /// <param name="selList">选中的购物车项键列表</param>
        /// <returns></returns>
        public CartModel ChangeProductCountForBook(int pid, int bookNumber, int uid, string selList)
        {
            try
            {
                //购物车商品列表
                List<OrderProductInfo> orderProductList = Carts.GetCartProductList(uid);
                //对应商品
                OrderProductInfo orderProductInfo = Carts.GetCommonOrderProductByPid(pid, orderProductList);
                if (orderProductInfo != null)//当商品已经存在
                {
                    if (bookNumber < 1)//当购买数量小于1时，删除此商品
                    {
                        Carts.DeleteCartProduct(ref orderProductList, orderProductInfo);
                    }
                    else if (bookNumber != orderProductInfo.BookNumber)
                    {
                        var days = (orderProductInfo.BookEndTime.Date - orderProductInfo.BookStartTime).Days;
                        orderProductInfo.BookNumber = bookNumber;
                        Carts.AddExistProductToCart(ref orderProductList, days * bookNumber, orderProductInfo, DateTime.Now);
                    }
                }

                //商品数量
                int pCount = Carts.SumOrderProductCount(orderProductList);
                //店铺购物车列表
                List<StoreCartInfo> storeCartList = Carts.TidyMallOrderProductList(StringHelper.SplitString(selList), orderProductList);

                //商品总数量
                int totalCount = Carts.SumMallCartOrderProductCount(storeCartList);
                //商品合计
                decimal productAmount = Carts.SumMallCartOrderProductAmount(storeCartList);
                //满减折扣
                int fullCut = Carts.SumMallCartFullCut(storeCartList);
                //订单合计
                decimal orderAmount = productAmount - fullCut;

                CartModel model = new CartModel
                {
                    TotalCount = totalCount,
                    ProductAmount = productAmount,
                    FullCut = fullCut,
                    OrderAmount = orderAmount,
                    StoreCartList = storeCartList
                };
                //返回将购物车中商品数量
                return model;

            }
            catch (Exception e)
            {
                Trace.TraceError(e.StackTrace);
                throw;
            }
        }

        #endregion

        /// <summary>
        /// 删除购物车中商品
        /// </summary>
        /// <param name="pid"></param>
        /// <param name="uid">用户ID</param>
        /// <param name="selList">选中的购物车项键列表</param>
        /// <returns></returns>
        public CartModel DelProduct(int pid, int uid, string selList)
        {
            try
            {
                //购物车商品列表
                List<OrderProductInfo> orderProductList = Carts.GetCartProductList(uid);
                //对应商品
                OrderProductInfo orderProductInfo = Carts.GetCommonOrderProductByPid(pid, orderProductList);
                if (orderProductInfo != null)
                    Carts.DeleteCartProduct(ref orderProductList, orderProductInfo);

                //商品数量
                int pCount = Carts.SumOrderProductCount(orderProductList);
                //店铺购物车列表
                List<StoreCartInfo> storeCartList = Carts.TidyMallOrderProductList(StringHelper.SplitString(selList), orderProductList);

                //商品总数量
                int totalCount = Carts.SumMallCartOrderProductCount(storeCartList);
                //商品合计
                decimal productAmount = Carts.SumMallCartOrderProductAmount(storeCartList);
                //满减折扣
                int fullCut = Carts.SumMallCartFullCut(storeCartList);
                //订单合计
                decimal orderAmount = productAmount - fullCut;

                CartModel model = new CartModel
                {
                    TotalCount = totalCount,
                    ProductAmount = productAmount,
                    FullCut = fullCut,
                    OrderAmount = orderAmount,
                    StoreCartList = storeCartList
                };
                return model;
            }
            catch (Exception e)
            {
                Trace.TraceError(e.StackTrace);
                throw;
            }
        }
        /// <summary>
        /// 删除购物车中商品
        /// </summary>
        /// <param name="pid"></param>
        /// <param name="sid">全局唯一标识符</param>
        /// <param name="selList">选中的购物车项键列表</param>
        /// <returns></returns>
        public CartModel DelSessionProduct(int pid, string sid, string selList)
        {
            if (string.IsNullOrEmpty(sid)) throw new ApiException(ExceptionCode.DataError);
            try
            {
                //购物车商品列表
                List<OrderProductInfo> orderProductList = Carts.GetCartProductList(sid).Where(t => !CartManager.CheckIsPrint(t.RecordId)).ToList();
                
                //对应商品
                OrderProductInfo orderProductInfo = Carts.GetCommonOrderProductByPid(pid, orderProductList);
                if (orderProductInfo != null)
                    Carts.DeleteCartProduct(ref orderProductList, orderProductInfo);
                if (orderProductList.Count == 0)
                    DiningTableManager.ClearCurrDiscount(int.Parse(sid));
                //商品数量
                int pCount = Carts.SumOrderProductCount(orderProductList);
                //店铺购物车列表
                List<StoreCartInfo> storeCartList = Carts.TidyMallOrderProductList(StringHelper.SplitString(selList), orderProductList);

                //商品总数量
                int totalCount = Carts.SumMallCartOrderProductCount(storeCartList);
                //商品合计
                decimal productAmount = Carts.SumMallCartOrderProductAmount(storeCartList);
                //满减折扣
                int fullCut = Carts.SumMallCartFullCut(storeCartList);
                //订单合计
                decimal orderAmount = productAmount - fullCut;

                CartModel model = new CartModel
                {
                    TotalCount = totalCount,
                    ProductAmount = productAmount,
                    FullCut = fullCut,
                    OrderAmount = orderAmount,
                    StoreCartList = storeCartList
                };
                return model;
            }
            catch (Exception e)
            {
                Trace.TraceError(e.StackTrace);
                throw;
            }
        }
        /// <summary>
        /// 店铺管理员删除菜品
        /// </summary>
        /// <param name="pid"></param>
        /// <param name="sid"></param>
        /// <param name="selList"></param>
        /// <returns></returns>
        public CartModel DelSessionProductByStoreAdmin(int pid, string sid, string selList)
        {
            if (string.IsNullOrEmpty(sid)) throw new ApiException(ExceptionCode.DataError);
            try
            {
                //购物车商品列表
                List<OrderProductInfo> orderProductList = Carts.GetCartProductList(sid).Where(t => CartManager.CheckIsPrint(t.RecordId)).ToList();

                //对应商品
                OrderProductInfo orderProductInfo = Carts.GetCommonOrderProductByPid(pid, orderProductList);
                if (orderProductInfo != null)
                    Carts.DeleteCartProduct(ref orderProductList, orderProductInfo);
                if (orderProductList.Count == 0)
                    DiningTableManager.ClearCurrDiscount(int.Parse(sid));
                //商品数量
                int pCount = Carts.SumOrderProductCount(orderProductList);
                //店铺购物车列表
                List<StoreCartInfo> storeCartList = Carts.TidyMallOrderProductList(StringHelper.SplitString(selList), orderProductList);

                //商品总数量
                int totalCount = Carts.SumMallCartOrderProductCount(storeCartList);
                //商品合计
                decimal productAmount = Carts.SumMallCartOrderProductAmount(storeCartList);
                //满减折扣
                int fullCut = Carts.SumMallCartFullCut(storeCartList);
                //订单合计
                decimal orderAmount = productAmount - fullCut;

                CartModel model = new CartModel
                {
                    TotalCount = totalCount,
                    ProductAmount = productAmount,
                    FullCut = fullCut,
                    OrderAmount = orderAmount,
                    StoreCartList = storeCartList
                };
                return model;
            }
            catch (Exception e)
            {
                Trace.TraceError(e.StackTrace);
                throw;
            }
        }
        /// <summary>
        /// 批量删除购物车中商品
        /// </summary>
        /// <param name="pids">都好分割的pid</param>
        /// <param name="uid">用户ID</param>
        /// <param name="selList">选中的购物车项键列表</param>
        /// <returns></returns>
        public CartModel DelProducts(string pids, int uid, string selList)
        {
            try
            {
                //购物车商品列表
                List<OrderProductInfo> orderProductList = Carts.GetCartProductList(uid);
                var ps = StringHelper.SplitString(pids);
                foreach (string t in ps)
                {
                    var pid = 0;
                    if (!int.TryParse(t, out pid)) continue;
                    //对应商品
                    OrderProductInfo orderProductInfo = Carts.GetCommonOrderProductByPid(pid, orderProductList);
                    if (orderProductInfo != null)
                        Carts.DeleteCartProduct(ref orderProductList, orderProductInfo);
                }
                //商品数量
                int pCount = Carts.SumOrderProductCount(orderProductList);
                //店铺购物车列表
                List<StoreCartInfo> storeCartList = Carts.TidyMallOrderProductList(StringHelper.SplitString(selList), orderProductList);

                //商品总数量
                int totalCount = Carts.SumMallCartOrderProductCount(storeCartList);
                //商品合计
                decimal productAmount = Carts.SumMallCartOrderProductAmount(storeCartList);
                //满减折扣
                int fullCut = Carts.SumMallCartFullCut(storeCartList);
                //订单合计
                decimal orderAmount = productAmount - fullCut;

                CartModel model = new CartModel
                {
                    TotalCount = totalCount,
                    ProductAmount = productAmount,
                    FullCut = fullCut,
                    OrderAmount = orderAmount,
                    StoreCartList = storeCartList
                };
                return model;
            }
            catch (Exception e)
            {
                Trace.TraceError(e.StackTrace);
                throw;
            }
        }
        /// <summary>
        /// 批量删除购物车中商品
        /// </summary>
        /// <param name="pids">都好分割的pid</param>
        /// <param name="sid">全局唯一标识符</param>
        /// <param name="selList">选中的购物车项键列表</param>
        /// <returns></returns>
        public CartModel DelSessionProducts(string pids, string sid, string selList)
        {
            if (string.IsNullOrEmpty(sid)) throw new ApiException(ExceptionCode.DataError);
            try
            {
                //购物车商品列表
                List<OrderProductInfo> orderProductList = Carts.GetCartProductList(sid).Where(t => !CartManager.CheckIsPrint(t.RecordId)).ToList(); ;
                var ps = StringHelper.SplitString(pids);
                foreach (string t in ps)
                {
                    var pid = 0;
                    if (!int.TryParse(t, out pid)) continue;
                    //对应商品
                    OrderProductInfo orderProductInfo = Carts.GetCommonOrderProductByPid(pid, orderProductList);
                    if (orderProductInfo != null)
                        Carts.DeleteCartProduct(ref orderProductList, orderProductInfo);
                }
                //商品数量
                int pCount = Carts.SumOrderProductCount(orderProductList);
                //店铺购物车列表
                List<StoreCartInfo> storeCartList = Carts.TidyMallOrderProductList(StringHelper.SplitString(selList), orderProductList);

                //商品总数量
                int totalCount = Carts.SumMallCartOrderProductCount(storeCartList);
                //商品合计
                decimal productAmount = Carts.SumMallCartOrderProductAmount(storeCartList);
                //满减折扣
                int fullCut = Carts.SumMallCartFullCut(storeCartList);
                //订单合计
                decimal orderAmount = productAmount - fullCut;

                CartModel model = new CartModel
                {
                    TotalCount = totalCount,
                    ProductAmount = productAmount,
                    FullCut = fullCut,
                    OrderAmount = orderAmount,
                    StoreCartList = storeCartList
                };
                return model;
            }
            catch (Exception e)
            {
                Trace.TraceError(e.StackTrace);
                throw;
            }
        }
        /// <summary>
        /// 添加套装到购物车
        /// </summary>
        /// <param name="pmId"></param>
        /// <param name="buyCount"></param>
        /// <param name="uid"></param>
        /// <returns></returns>
        public int AddSuit(int pmId, int buyCount, int uid)
        {
            try
            {
                //购买数量不能小于1
                if (buyCount < 1)
                    throw new Exception("请填写购买数量");
                //获得套装促销活动
                SuitPromotionInfo suitPromotionInfo = Promotions.GetSuitPromotionByPmIdAndTime(pmId, DateTime.Now);
                //套装促销活动不存在时
                if (suitPromotionInfo == null)
                    throw new Exception("请选择套装");

                //店铺信息
                StoreInfo storeInfo = Stores.GetStoreById(suitPromotionInfo.StoreId);
                if (storeInfo.State != (int)StoreState.Open)
                    throw new Exception("请选择套装");

                //购物车商品列表
                List<OrderProductInfo> orderProductList = Carts.GetCartProductList(uid);
                List<OrderProductInfo> suitOrderProductList = Carts.GetSuitOrderProductList(pmId, orderProductList, false);


                //扩展套装商品列表
                List<ExtSuitProductInfo> extSuitProductList = Promotions.GetExtSuitProductList(suitPromotionInfo.PmId);
                if (extSuitProductList.Count < 1)
                    throw new Exception("套装中没有商品");

                //套装商品id列表
                StringBuilder pidList = new StringBuilder();
                foreach (ExtSuitProductInfo extSuitProductInfo in extSuitProductList)
                    pidList.AppendFormat("{0},", extSuitProductInfo.Pid);
                pidList.Remove(pidList.Length - 1, 1);

                //套装商品库存列表
                List<ProductStockInfo> productStockList = Products.GetProductStockList(pidList.ToString());
                foreach (ProductStockInfo item in productStockList)
                {
                    if (item.Number < buyCount)
                        throw new Exception("商品库存不够,商品ID:" + item.Pid);
                }

                buyCount = suitOrderProductList.Count == 0 ? buyCount : suitOrderProductList[0].BuyCount / suitOrderProductList[0].ExtCode2 + buyCount;
                Carts.AddSuitToCart(ref orderProductList, extSuitProductList, suitPromotionInfo, buyCount, "", uid, DateTime.Now);
                //返回购物车中商品数量
                return Carts.SumOrderProductCount(orderProductList);
            }
            catch (Exception e)
            {
                Trace.TraceError(e.StackTrace);
                throw;
            }
        }
        /// <summary>
        /// 添加套装到购物车
        /// </summary>
        /// <param name="pmId"></param>
        /// <param name="buyCount"></param>
        /// <param name="sid">全局唯一标识符</param>
        /// <returns></returns>
        public int AddSessionSuit(int pmId, int buyCount, string sid)
        {
            if (string.IsNullOrEmpty(sid)) throw new ApiException(ExceptionCode.DataError);
            try
            {
                //购买数量不能小于1
                if (buyCount < 1)
                    throw new Exception("请填写购买数量");
                //获得套装促销活动
                SuitPromotionInfo suitPromotionInfo = Promotions.GetSuitPromotionByPmIdAndTime(pmId, DateTime.Now);
                //套装促销活动不存在时
                if (suitPromotionInfo == null)
                    throw new Exception("请选择套装");

                //店铺信息
                StoreInfo storeInfo = Stores.GetStoreById(suitPromotionInfo.StoreId);
                if (storeInfo.State != (int)StoreState.Open)
                    throw new Exception("请选择套装");

                //购物车商品列表
                List<OrderProductInfo> orderProductList = Carts.GetCartProductList(sid).Where(t => !CartManager.CheckIsPrint(t.RecordId)).ToList(); ;
                List<OrderProductInfo> suitOrderProductList = Carts.GetSuitOrderProductList(pmId, orderProductList, false);


                //扩展套装商品列表
                List<ExtSuitProductInfo> extSuitProductList = Promotions.GetExtSuitProductList(suitPromotionInfo.PmId);
                if (extSuitProductList.Count < 1)
                    throw new Exception("套装中没有商品");

                //套装商品id列表
                StringBuilder pidList = new StringBuilder();
                foreach (ExtSuitProductInfo extSuitProductInfo in extSuitProductList)
                    pidList.AppendFormat("{0},", extSuitProductInfo.Pid);
                pidList.Remove(pidList.Length - 1, 1);

                //套装商品库存列表
                List<ProductStockInfo> productStockList = Products.GetProductStockList(pidList.ToString());
                foreach (ProductStockInfo item in productStockList)
                {
                    if (item.Number < buyCount)
                        throw new Exception("商品库存不够,商品ID:" + item.Pid);
                }

                buyCount = suitOrderProductList.Count == 0 ? buyCount : suitOrderProductList[0].BuyCount / suitOrderProductList[0].ExtCode2 + buyCount;
                Carts.AddSuitToCart(ref orderProductList, extSuitProductList, suitPromotionInfo, buyCount, sid, 0, DateTime.Now);
                //返回购物车中商品数量
                return Carts.SumOrderProductCount(orderProductList);
            }
            catch (Exception e)
            {
                Trace.TraceError(e.StackTrace);
                throw;
            }
        }
        /// <summary>
        /// 修改购物车中套装数量
        /// </summary>
        /// <param name="pmId"></param>
        /// <param name="buyCount"></param>
        /// <param name="uid"></param>
        /// <param name="selList">选中的购物车项键列表</param>
        /// <returns></returns>
        public CartModel ChangeSuitCount(int pmId, int buyCount, int uid, string selList)
        {
            try
            {
                //购物车商品列表
                List<OrderProductInfo> orderProductList = Carts.GetCartProductList(uid);
                //套装商品列表
                List<OrderProductInfo> suitOrderProductList = Carts.GetSuitOrderProductList(pmId, orderProductList, true);
                if (suitOrderProductList.Count > 0)//当套装已经存在
                {
                    if (buyCount < 1)//当购买数量小于1时，删除此套装
                    {
                        Carts.DeleteCartSuit(ref orderProductList, pmId);
                    }
                    else
                    {
                        OrderProductInfo orderProductInfo = suitOrderProductList.Find(x => x.Type == 3);
                        int oldBuyCount = orderProductInfo.RealCount / orderProductInfo.ExtCode2;
                        if (buyCount != oldBuyCount)
                            Carts.AddExistSuitToCart(ref orderProductList, suitOrderProductList, buyCount);
                    }
                }

                //商品数量
                int pCount = Carts.SumOrderProductCount(orderProductList);
                //店铺购物车列表
                List<StoreCartInfo> storeCartList = Carts.TidyMallOrderProductList(StringHelper.SplitString(selList), orderProductList);

                //商品总数量
                int totalCount = Carts.SumMallCartOrderProductCount(storeCartList);
                //商品合计
                decimal productAmount = Carts.SumMallCartOrderProductAmount(storeCartList);
                //满减折扣
                int fullCut = Carts.SumMallCartFullCut(storeCartList);
                //订单合计
                decimal orderAmount = productAmount - fullCut;

                CartModel model = new CartModel
                {
                    TotalCount = totalCount,
                    ProductAmount = productAmount,
                    FullCut = fullCut,
                    OrderAmount = orderAmount,
                    StoreCartList = storeCartList
                };
                return model;
            }
            catch (Exception e)
            {
                Trace.TraceError(e.StackTrace);
                throw;
            }
        }
        /// <summary>
        /// 修改购物车中套装数量
        /// </summary>
        /// <param name="pmId"></param>
        /// <param name="buyCount"></param>
        /// <param name="sid">全局唯一标识符</param>
        /// <param name="selList">选中的购物车项键列表</param>
        /// <returns></returns>
        public CartModel ChangeSessionSuitCount(int pmId, int buyCount, string sid, string selList)
        {
            if (string.IsNullOrEmpty(sid)) throw new ApiException(ExceptionCode.DataError);
            try
            {
                //购物车商品列表
                List<OrderProductInfo> orderProductList = Carts.GetCartProductList(sid).Where(t => !CartManager.CheckIsPrint(t.RecordId)).ToList(); ;
                //套装商品列表
                List<OrderProductInfo> suitOrderProductList = Carts.GetSuitOrderProductList(pmId, orderProductList, true);
                if (suitOrderProductList.Count > 0)//当套装已经存在
                {
                    if (buyCount < 1)//当购买数量小于1时，删除此套装
                    {
                        Carts.DeleteCartSuit(ref orderProductList, pmId);
                    }
                    else
                    {
                        OrderProductInfo orderProductInfo = suitOrderProductList.Find(x => x.Type == 3);
                        int oldBuyCount = orderProductInfo.RealCount / orderProductInfo.ExtCode2;
                        if (buyCount != oldBuyCount)
                            Carts.AddExistSuitToCart(ref orderProductList, suitOrderProductList, buyCount);
                    }
                }

                //商品数量
                int pCount = Carts.SumOrderProductCount(orderProductList);
                //店铺购物车列表
                List<StoreCartInfo> storeCartList = Carts.TidyMallOrderProductList(StringHelper.SplitString(selList), orderProductList);

                //商品总数量
                int totalCount = Carts.SumMallCartOrderProductCount(storeCartList);
                //商品合计
                decimal productAmount = Carts.SumMallCartOrderProductAmount(storeCartList);
                //满减折扣
                int fullCut = Carts.SumMallCartFullCut(storeCartList);
                //订单合计
                decimal orderAmount = productAmount - fullCut;

                CartModel model = new CartModel
                {
                    TotalCount = totalCount,
                    ProductAmount = productAmount,
                    FullCut = fullCut,
                    OrderAmount = orderAmount,
                    StoreCartList = storeCartList
                };
                return model;
            }
            catch (Exception e)
            {
                Trace.TraceError(e.StackTrace);
                throw;
            }
        }
        /// <summary>
        /// 删除购物车中套装
        /// </summary>
        /// <param name="pmId"></param>
        /// <param name="uid"></param>
        /// <param name="selList">选中的购物车项键列表</param>
        /// <returns></returns>
        public CartModel DelSuit(int pmId, int uid, string selList)
        {
            try
            {
                //购物车商品列表
                List<OrderProductInfo> orderProductList = Carts.GetCartProductList(uid);
                Carts.DeleteCartSuit(ref orderProductList, pmId);

                //商品数量
                int pCount = Carts.SumOrderProductCount(orderProductList);
                //店铺购物车列表
                List<StoreCartInfo> storeCartList = Carts.TidyMallOrderProductList(StringHelper.SplitString(selList), orderProductList);

                //商品总数量
                int totalCount = Carts.SumMallCartOrderProductCount(storeCartList);
                //商品合计
                decimal productAmount = Carts.SumMallCartOrderProductAmount(storeCartList);
                //满减折扣
                int fullCut = Carts.SumMallCartFullCut(storeCartList);
                //订单合计
                decimal orderAmount = productAmount - fullCut;

                CartModel model = new CartModel
                {
                    TotalCount = totalCount,
                    ProductAmount = productAmount,
                    FullCut = fullCut,
                    OrderAmount = orderAmount,
                    StoreCartList = storeCartList
                };
                return model;
            }
            catch (Exception e)
            {
                Trace.TraceError(e.StackTrace);
                throw;
            }
        }
        /// <summary>
        /// 删除购物车中套装
        /// </summary>
        /// <param name="pmId"></param>
        /// <param name="sid">全局唯一标识符</param>
        /// <param name="selList">选中的购物车项键列表</param>
        /// <returns></returns>
        public CartModel DelSessionSuit(int pmId, string sid, string selList)
        {
            if (string.IsNullOrEmpty(sid)) throw new ApiException(ExceptionCode.DataError);
            try
            {
                //购物车商品列表
                List<OrderProductInfo> orderProductList = Carts.GetCartProductList(sid).Where(t => !CartManager.CheckIsPrint(t.RecordId)).ToList(); ;
                Carts.DeleteCartSuit(ref orderProductList, pmId);

                //商品数量
                int pCount = Carts.SumOrderProductCount(orderProductList);
                //店铺购物车列表
                List<StoreCartInfo> storeCartList = Carts.TidyMallOrderProductList(StringHelper.SplitString(selList), orderProductList);

                //商品总数量
                int totalCount = Carts.SumMallCartOrderProductCount(storeCartList);
                //商品合计
                decimal productAmount = Carts.SumMallCartOrderProductAmount(storeCartList);
                //满减折扣
                int fullCut = Carts.SumMallCartFullCut(storeCartList);
                //订单合计
                decimal orderAmount = productAmount - fullCut;

                CartModel model = new CartModel
                {
                    TotalCount = totalCount,
                    ProductAmount = productAmount,
                    FullCut = fullCut,
                    OrderAmount = orderAmount,
                    StoreCartList = storeCartList
                };
                return model;
            }
            catch (Exception e)
            {
                Trace.TraceError(e.StackTrace);
                throw;
            }
        }
        /// <summary>
        /// 获得满赠赠品
        /// </summary>
        /// <param name="pmId">满赠id</param>
        /// <returns></returns>
        public List<PartProductInfo> GetFullSend(int pmId)
        {
            try
            {
                //获得满赠赠品列表
                List<PartProductInfo> fullSendPresentList = Promotions.GetFullSendPresentList(pmId);

                return fullSendPresentList;
            }
            catch (Exception e)
            {
                Trace.TraceError(e.StackTrace);
                throw;
            }
        }
        /// <summary>
        /// 添加满赠到购物车
        /// </summary>
        /// <param name="pmId">满赠id</param>
        /// <param name="pid">商品id</param>
        /// <param name="uid">用户ID</param>
        /// <param name="selList">选中的购物车项键列表</param>
        /// <returns></returns>
        public CartModel AddFullSend(int pmId, int pid, int uid, string selList)
        {
            try
            {
                //添加的商品
                PartProductInfo partProductInfo = Products.GetPartProductById(pid);
                if (partProductInfo == null)
                    throw new Exception("请选择商品");

                //店铺信息
                StoreInfo storeInfo = Stores.GetStoreById(partProductInfo.StoreId);
                if (storeInfo.State != (int)StoreState.Open)
                    throw new Exception("请选择商品");

                //商品库存
                int stockNumber = Products.GetProductStockNumberByPid(pid);
                if (stockNumber < 1)
                    throw new Exception("商品库存不足");

                //满赠促销活动
                FullSendPromotionInfo fullSendPromotionInfo = Promotions.GetFullSendPromotionByPmIdAndTime(pmId, DateTime.Now);
                if (fullSendPromotionInfo == null)
                    throw new Exception("满赠促销活动不存在或已经结束");

                //购物车商品列表
                List<OrderProductInfo> orderProductList = Carts.GetCartProductList(uid);

                //满赠主商品列表
                List<OrderProductInfo> fullSendMainOrderProductList = Carts.GetFullSendMainOrderProductList(pmId, orderProductList);
                if (fullSendMainOrderProductList.Count < 1)
                    throw new Exception("不符合活动条件");
                decimal amount = Carts.SumOrderProductAmount(fullSendMainOrderProductList);
                if (fullSendPromotionInfo.LimitMoney > amount)
                    throw new Exception("不符合活动条件");

                if (!Promotions.IsExistFullSendProduct(pmId, pid, 1))
                    throw new Exception("此商品不是满赠商品");

                //赠送商品
                OrderProductInfo fullSendMinorOrderProductInfo = Carts.GetFullSendMinorOrderProduct(pmId, orderProductList);
                if (fullSendMinorOrderProductInfo != null)
                {
                    if (fullSendMinorOrderProductInfo.Pid != pid)
                        Carts.DeleteCartFullSend(ref orderProductList, fullSendMinorOrderProductInfo);
                    else
                        throw new Exception("此商品已经添加");
                }

                //添加满赠商品
                Carts.AddFullSendToCart(ref orderProductList, partProductInfo, fullSendPromotionInfo, "", uid, DateTime.Now);

                //商品数量
                int pCount = Carts.SumOrderProductCount(orderProductList);
                //店铺购物车列表
                List<StoreCartInfo> storeCartList = Carts.TidyMallOrderProductList(StringHelper.SplitString(selList), orderProductList);

                //商品总数量
                int totalCount = Carts.SumMallCartOrderProductCount(storeCartList);
                //商品合计
                decimal productAmount = Carts.SumMallCartOrderProductAmount(storeCartList);
                //满减折扣
                int fullCut = Carts.SumMallCartFullCut(storeCartList);
                //订单合计
                decimal orderAmount = productAmount - fullCut;

                CartModel model = new CartModel
                {
                    TotalCount = totalCount,
                    ProductAmount = productAmount,
                    FullCut = fullCut,
                    OrderAmount = orderAmount,
                    StoreCartList = storeCartList
                };
                return model;
            }
            catch (Exception e)
            {
                Trace.TraceError(e.StackTrace);
                throw;
            }
        }
        /// <summary>
        /// 添加满赠到购物车
        /// </summary>
        /// <param name="pmId">满赠id</param>
        /// <param name="pid">商品id</param>
        /// <param name="sid">全局唯一标识符</param>
        /// <param name="selList">选中的购物车项键列表</param>
        /// <returns></returns>
        public CartModel AddSessionFullSend(int pmId, int pid, string sid, string selList)
        {
            if (string.IsNullOrEmpty(sid)) throw new ApiException(ExceptionCode.DataError);
            try
            {
                //添加的商品
                PartProductInfo partProductInfo = Products.GetPartProductById(pid);
                if (partProductInfo == null)
                    throw new Exception("请选择商品");

                //店铺信息
                StoreInfo storeInfo = Stores.GetStoreById(partProductInfo.StoreId);
                if (storeInfo.State != (int)StoreState.Open)
                    throw new Exception("请选择商品");

                //商品库存
                int stockNumber = Products.GetProductStockNumberByPid(pid);
                if (stockNumber < 1)
                    throw new Exception("商品库存不足");

                //满赠促销活动
                FullSendPromotionInfo fullSendPromotionInfo = Promotions.GetFullSendPromotionByPmIdAndTime(pmId, DateTime.Now);
                if (fullSendPromotionInfo == null)
                    throw new Exception("满赠促销活动不存在或已经结束");

                //购物车商品列表
                List<OrderProductInfo> orderProductList = Carts.GetCartProductList(sid).Where(t => !CartManager.CheckIsPrint(t.RecordId)).ToList(); ;

                //满赠主商品列表
                List<OrderProductInfo> fullSendMainOrderProductList = Carts.GetFullSendMainOrderProductList(pmId, orderProductList);
                if (fullSendMainOrderProductList.Count < 1)
                    throw new Exception("不符合活动条件");
                decimal amount = Carts.SumOrderProductAmount(fullSendMainOrderProductList);
                if (fullSendPromotionInfo.LimitMoney > amount)
                    throw new Exception("不符合活动条件");

                if (!Promotions.IsExistFullSendProduct(pmId, pid, 1))
                    throw new Exception("此商品不是满赠商品");

                //赠送商品
                OrderProductInfo fullSendMinorOrderProductInfo = Carts.GetFullSendMinorOrderProduct(pmId, orderProductList);
                if (fullSendMinorOrderProductInfo != null)
                {
                    if (fullSendMinorOrderProductInfo.Pid != pid)
                        Carts.DeleteCartFullSend(ref orderProductList, fullSendMinorOrderProductInfo);
                    else
                        throw new Exception("此商品已经添加");
                }

                //添加满赠商品
                Carts.AddFullSendToCart(ref orderProductList, partProductInfo, fullSendPromotionInfo, sid, 0, DateTime.Now);

                //商品数量
                int pCount = Carts.SumOrderProductCount(orderProductList);
                //店铺购物车列表
                List<StoreCartInfo> storeCartList = Carts.TidyMallOrderProductList(StringHelper.SplitString(selList), orderProductList);

                //商品总数量
                int totalCount = Carts.SumMallCartOrderProductCount(storeCartList);
                //商品合计
                decimal productAmount = Carts.SumMallCartOrderProductAmount(storeCartList);
                //满减折扣
                int fullCut = Carts.SumMallCartFullCut(storeCartList);
                //订单合计
                decimal orderAmount = productAmount - fullCut;

                CartModel model = new CartModel
                {
                    TotalCount = totalCount,
                    ProductAmount = productAmount,
                    FullCut = fullCut,
                    OrderAmount = orderAmount,
                    StoreCartList = storeCartList
                };
                return model;
            }
            catch (Exception e)
            {
                Trace.TraceError(e.StackTrace);
                throw;
            }
        }

        /// <summary>
        /// 删除购物车中满赠
        /// </summary>
        /// <param name="pmId">满赠id</param>
        /// <param name="uid">用户ID</param>
        /// <param name="selList">选中的购物车项键列表</param>
        /// <returns></returns>
        public CartModel DelFullSend(int pmId, int uid, string selList)
        {
            try
            {
                //购物车商品列表
                List<OrderProductInfo> orderProductList = Carts.GetCartProductList(uid);
                //删除满赠
                Carts.DeleteCartFullSend(ref orderProductList, pmId);

                //商品数量
                int pCount = Carts.SumOrderProductCount(orderProductList);
                //店铺购物车列表
                List<StoreCartInfo> storeCartList = Carts.TidyMallOrderProductList(StringHelper.SplitString(selList), orderProductList);

                //商品总数量
                int totalCount = Carts.SumMallCartOrderProductCount(storeCartList);
                //商品合计
                decimal productAmount = Carts.SumMallCartOrderProductAmount(storeCartList);
                //满减折扣
                int fullCut = Carts.SumMallCartFullCut(storeCartList);
                //订单合计
                decimal orderAmount = productAmount - fullCut;

                CartModel model = new CartModel
                {
                    TotalCount = totalCount,
                    ProductAmount = productAmount,
                    FullCut = fullCut,
                    OrderAmount = orderAmount,
                    StoreCartList = storeCartList
                };
                return model;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
        /// <summary>
        /// 删除购物车中满赠
        /// </summary>
        /// <param name="pmId">满赠id</param>
        /// <param name="sid">全局唯一标识符</param>
        /// <param name="selList">选中的购物车项键列表</param>
        /// <returns></returns>
        public CartModel DelSessionFullSend(int pmId, string sid, string selList)
        {
            if (string.IsNullOrEmpty(sid)) throw new ApiException(ExceptionCode.DataError);
            try
            {
                //购物车商品列表
                List<OrderProductInfo> orderProductList = Carts.GetCartProductList(sid).Where(t => !CartManager.CheckIsPrint(t.RecordId)).ToList(); ;
                //删除满赠
                Carts.DeleteCartFullSend(ref orderProductList, pmId);

                //商品数量
                int pCount = Carts.SumOrderProductCount(orderProductList);
                //店铺购物车列表
                List<StoreCartInfo> storeCartList = Carts.TidyMallOrderProductList(StringHelper.SplitString(selList), orderProductList);

                //商品总数量
                int totalCount = Carts.SumMallCartOrderProductCount(storeCartList);
                //商品合计
                decimal productAmount = Carts.SumMallCartOrderProductAmount(storeCartList);
                //满减折扣
                int fullCut = Carts.SumMallCartFullCut(storeCartList);
                //订单合计
                decimal orderAmount = productAmount - fullCut;

                CartModel model = new CartModel
                {
                    TotalCount = totalCount,
                    ProductAmount = productAmount,
                    FullCut = fullCut,
                    OrderAmount = orderAmount,
                    StoreCartList = storeCartList
                };
                return model;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
        /// <summary>
        ///  取消或选中购物车项
        /// </summary>
        /// <param name="uid">用户ID</param>
        /// <param name="selList">选中的购物车项键列表</param>
        /// <returns></returns>
        public CartModel CancelOrSelectCartItem(int uid, string selList)
        {
            try
            {
                //购物车商品列表
                List<OrderProductInfo> orderProductList = Carts.GetCartProductList(uid);
                //店铺购物车列表
                List<StoreCartInfo> storeCartList = Carts.TidyMallOrderProductList(StringHelper.SplitString(selList), orderProductList);

                //商品总数量
                int totalCount = Carts.SumMallCartOrderProductCount(storeCartList);
                //商品合计
                decimal productAmount = Carts.SumMallCartOrderProductAmount(storeCartList);
                //满减折扣
                int fullCut = Carts.SumMallCartFullCut(storeCartList);
                //订单合计
                decimal orderAmount = productAmount - fullCut;

                CartModel model = new CartModel
                {
                    TotalCount = totalCount,
                    ProductAmount = productAmount,
                    FullCut = fullCut,
                    OrderAmount = orderAmount,
                    StoreCartList = storeCartList
                };
                return model;
            }
            catch (Exception e)
            {
                Trace.TraceError(e.StackTrace);
                throw;
            }
        }
        /// <summary>
        ///  取消或选中购物车项
        /// </summary>
        /// <param name="sid">全局唯一标识符</param>
        /// <param name="selList">选中的购物车项键列表</param>
        /// <returns></returns>
        public CartModel CancelOrSelectSessionCartItem(string sid, string selList)
        {
            if (string.IsNullOrEmpty(sid)) throw new ApiException(ExceptionCode.DataError);
            try
            {
                //购物车商品列表
                List<OrderProductInfo> orderProductList = Carts.GetCartProductList(sid).Where(t => !CartManager.CheckIsPrint(t.RecordId)).ToList(); ;
                //店铺购物车列表
                List<StoreCartInfo> storeCartList = Carts.TidyMallOrderProductList(StringHelper.SplitString(selList), orderProductList);

                //商品总数量
                int totalCount = Carts.SumMallCartOrderProductCount(storeCartList);
                //商品合计
                decimal productAmount = Carts.SumMallCartOrderProductAmount(storeCartList);
                //满减折扣
                int fullCut = Carts.SumMallCartFullCut(storeCartList);
                //订单合计
                decimal orderAmount = productAmount - fullCut;

                CartModel model = new CartModel
                {
                    TotalCount = totalCount,
                    ProductAmount = productAmount,
                    FullCut = fullCut,
                    OrderAmount = orderAmount,
                    StoreCartList = storeCartList
                };
                return model;
            }
            catch (Exception e)
            {
                Trace.TraceError(e.StackTrace);
                throw;
            }
        }
        /// <summary>
        /// 选中全部购物车项
        /// </summary>
        /// <param name="uid">用户ID</param>
        /// <returns></returns>
        public CartModel SelectAllCartItem(int uid)
        {
            try
            {
                //购物车商品列表
                List<OrderProductInfo> orderProductList = Carts.GetCartProductList(uid);
                //店铺购物车列表
                List<StoreCartInfo> storeCartList = Carts.TidyMallOrderProductList(orderProductList);

                //商品总数量
                int totalCount = Carts.SumMallCartOrderProductCount(storeCartList);
                //商品合计
                decimal productAmount = Carts.SumMallCartOrderProductAmount(storeCartList);
                //满减折扣
                int fullCut = Carts.SumMallCartFullCut(storeCartList);
                //订单合计
                decimal orderAmount = productAmount - fullCut;

                CartModel model = new CartModel
                {
                    TotalCount = totalCount,
                    ProductAmount = productAmount,
                    FullCut = fullCut,
                    OrderAmount = orderAmount,
                    StoreCartList = storeCartList
                };
                return model;
            }
            catch (Exception e)
            {
                Trace.TraceError(e.StackTrace);
                throw;
            }
        }
        /// <summary>
        /// 选中全部购物车项
        /// </summary>
        /// <param name="sid">全局唯一标识符</param>
        /// <returns></returns>
        public CartModel SelectAllSessionCartItem(string sid)
        {
            if (string.IsNullOrEmpty(sid)) throw new ApiException(ExceptionCode.DataError);
            try
            {
                //购物车商品列表
                List<OrderProductInfo> orderProductList = Carts.GetCartProductList(sid).Where(t => !CartManager.CheckIsPrint(t.RecordId)).ToList(); ;
                //店铺购物车列表
                List<StoreCartInfo> storeCartList = Carts.TidyMallOrderProductList(orderProductList);

                //商品总数量
                int totalCount = Carts.SumMallCartOrderProductCount(storeCartList);
                //商品合计
                decimal productAmount = Carts.SumMallCartOrderProductAmount(storeCartList);
                //满减折扣
                int fullCut = Carts.SumMallCartFullCut(storeCartList);
                //订单合计
                decimal orderAmount = productAmount - fullCut;

                CartModel model = new CartModel
                {
                    TotalCount = totalCount,
                    ProductAmount = productAmount,
                    FullCut = fullCut,
                    OrderAmount = orderAmount,
                    StoreCartList = storeCartList
                };
                return model;
            }
            catch (Exception e)
            {
                Trace.TraceError(e.StackTrace);
                throw;
            }
        }
    }
}
